package com.example.demo.service;

import com.example.demo.data.SxConfig;
import com.example.demo.vo.Boutlist;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ltian on 2020/1/20.
 */
@Service
@Slf4j
public class PackService {

    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    private SxConfig config;

    //按天处理完所有数据后执行一次，将失败的设为未打包
    public void afterBatchHandle(int squaddate) {
        if (config.isReadonly()) {
            return;
        }

        int affected = jdbcTemplate.update(
                "UPDATE  " + packTable() + " SET upstatus = 0 WHERE upstatus = 2 AND tablename = ? AND datatype = ? AND transdate = ?",
                new Object[]{config.getBoutlist(), config.getDataType(), squaddate}
        );

        log.info("afterBatchHandle, affected:{}", affected);
    }

    // 将数据标记为无效的场合，将该条数据对应的打包状态为未提交的总数和金额更新
    public void afterAbort(Boutlist boutlist) {
        List<Map<String, Object>> packs = jdbcTemplate.queryForList(
                "SELECT * FROM " + packTable() + " WHERE upstatus = 0 AND tablename = ? AND datatype = ? AND transdate = ? AND packname = ? ",
                new Object[]{config.getBoutlist(), config.getDataType(), boutlist.getSquaddate(), boutlist.getCtranspackname()});

        if (packs.isEmpty()) {
            return;
        }
        if (packs.size() > 1) {
            throw new RuntimeException("multi pack record found");
        }
        Map<String, Object> pack = packs.get(0);

        long packCount = ((Number) pack.get("PACKCOUNT")).longValue();
        long amount = ((Number) pack.get("AMOUNT")).longValue();

        log.info("===> update {} after abort, listno: {}, ctranspackname:{} ", packTable(), boutlist.getListno(), boutlist.getCtranspackname());
        if (config.isReadonly()) {
            return;
        }
        int affected;
        if (packCount > 1) {
            affected = jdbcTemplate.update(
                    "UPDATE " + packTable() + " SET amount = ?, packcount = ? WHERE upstatus = 0 AND tablename = ? AND datatype = ? AND transdate = ? AND packname = ? ",
                    new Object[]{amount - boutlist.getTotaltoll(), packCount - 1, config.getBoutlist(), config.getDataType(), boutlist.getSquaddate(), boutlist.getCtranspackname()}
            );
        } else {
            affected = jdbcTemplate.update(
                    "DELETE FROM " + packTable() + " WHERE upstatus = 0 AND tablename = ? AND datatype = ? AND transdate = ? AND packname = ? ",
                    new Object[]{config.getBoutlist(), config.getDataType(), boutlist.getSquaddate(), boutlist.getCtranspackname()}
            );
        }
        if (affected != 1) {
            throw new RuntimeException("update pack status error, listno: " + boutlist.getListno() + "affected: " + affected);
        }
    }

    private String packTable() {
        if (config.isETC()) {
            return "clear_pack_muletc_main";
        }
        return "clear_pack_mulmtc_main";
    }


}
